<?php

class User_IndexController extends Fengqiaoju_Controller_Action {

	public function init() {

		parent::init ();
		
		$this->view->userObject = $this->userObject;
		$this->view->title = '用户中心';
	}

	//首页
	public function indexAction() {

		if (! $this->isLogin ()) {
			$this->_redirect ( '/user/index/login' );
		}
	}

	//退出
	public function logoutAction() {

		$this->_helper->viewRenderer->setNoRender ();
		$this->userObject->logout ();
		$this->_redirect ( '/user/index/login' );
	}

	//登入
	public function loginAction() {

		$this->isLogin ( '/' );
		
		if ($this->getRequest ()->isPost ()) {
			$user = $this->view->userObject->login ( $_POST ['username'], $_POST ['password'] );
			/*
        	if($this->codeSession->code<>$_POST['yzm']) {
				$msg = array(
					'msg'=>'验证码错误'
				);	
				$uid=0;
			}*/
			
			if ($user ['uid'] > 0) {
				$this->_redirect ( '/user' );
			}
			elseif ($user ['uid'] == - 1) {
				$this->message ( '用户名不存在或者被删除' );
			}
			elseif ($user ['uid'] == - 2) {
				$this->message ( '密码错误' );
			}
			else {
				$this->message ( '登入失败' );
			}
		}
		else {
			//$this->view->yzm = $this->yzm();//产生验证码
		

		}
	}

	//注册
	public function registerAction() {

		if ($this->getRequest ()->isPost ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$uid = 0;
			$uid = $this->userObject->register ( $_POST ['username'], $_POST ['password'], $_POST ['email'] );
			if ($uid <= 0) {
				if ($uid == - 1) {
					$this->message ( '用户名不合法' );
				}
				elseif ($uid == - 2) {
					$this->message ( '包含要允许注册的词语' );
				}
				elseif ($uid == - 3) {
					$this->message ( '用户名已经存在' );
				}
				elseif ($uid == - 4) {
					$this->message ( 'Email 格式有误' );
				}
				elseif ($uid == - 5) {
					$this->message ( 'Email 不允许注册' );
				}
				elseif ($uid == - 6) {
					$this->message ( '该 Email 已经被注册' );
				}
			}
			else {
				list ( $uid , $username , $password , $email ) = $this->view->userObject->login ( $_POST ['username'], $_POST ['password'] );
				$this->_redirect ( '/user' );
			}
		}
		else {
			//$this->view->yzm = $this->yzm ();
		

		}
	}

	/*
	 * 验证
	 */
	public function checkAction() {

		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender ();
		$type = $this->_request->get ( 'type' );
		$value = $this->_request->get ( 'value' );
		switch ($type) {
			case 'email' :
				$rs = $this->userObject->checkEmail ( $value );
				if ($rs > 0) {
					echo "<span  style='float:left;margin:10px 0;'><img src='/source/images/ok.gif'></span>";
				}
				elseif ($rs == - 4) {
					echo '<span  class="info">Email 格式有误</span>';
				}
				elseif ($rs == - 5) {
					echo '<span  class="info">Email 不允许注册</span>';
				}
				elseif ($rs == - 6) {
					echo '<span  class="info">该 Email 已经被注册</span>';
				}
				break;
			case 'username' :
				$rs = $this->userObject->checkUserName ( $value );
				if ($rs > 0) {
					echo "<span  style='float:left;margin:10px 0;'><img src='/source/images/ok.gif'></span>";
				}
				elseif ($rs == - 1) {
					echo '<span  class="info">用户名不合法</span>';
				}
				elseif ($rs == - 2) {
					echo '<span  class="info">包含要允许注册的词语</span>';
				}
				elseif ($rs == - 3) {
					echo '<span class="info">用户名已经存在</span>';
				}
		}
	
	}

}

